package view.code
{
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.hexagonstar.util.debug.Debug;
	
	import define.CharacterConst;
	
	import flash.events.MouseEvent;
	
	import mvc.control.CharacterEvent;
	
	import mx.collections.ArrayCollection;
	import mx.containers.VBox;
	import mx.controls.Button;
	import mx.controls.ComboBox;
	import mx.events.FlexEvent;
	import mx.events.ListEvent;
	
	import orm.fantasy.TblCharacterVO;

	public class CodeNewCharacter extends VBox
	{
		public var cobClass:ComboBox;
		public var cobType:ComboBox;
		public var btnFinish:Button;
		public var btnCancel:Button;

		public function CodeNewCharacter()
		{
			super();

			this.addEventListener(FlexEvent.INITIALIZE, design_initializeHandler);
		}
		private var _oldIndex:int;

		public function set oldIndex(data:int):void
		{
			_oldIndex = data;

			super.invalidateProperties();
		}

		override protected function commitProperties():void
		{
			super.commitProperties();

			this.btnCancel.enabled = _oldIndex != -1;
		}

		protected function design_initializeHandler(event:FlexEvent):void
		{
			this.cobClass.dataProvider = CharacterConst.getAllClasses();
		}

		protected function cobClass_changeHandler(event:ListEvent):void
		{
			var propClass:String = this.cobClass.selectedLabel;
//			Debug.trace("cobClass_changeHandler: " + propClass);
			this.cobType.dataProvider = CharacterConst.getTypes(propClass);
			(this.cobType.dataProvider as ArrayCollection).refresh();

			this.btnFinish.enabled = false;
		}

		protected function cobType_changeHandler(event:ListEvent):void
		{
			this.btnFinish.enabled = true;
		}

		protected function btnFinish_clickHandler(event:MouseEvent):void
		{
			var vo:TblCharacterVO = new TblCharacterVO();
			vo.prop_class = this.cobClass.selectedLabel;
			vo.prop_type = this.cobType.selectedLabel;
			vo.char_name = this.cobClass.selectedLabel + " - " + this.cobType.selectedLabel;

			var mvcEvent:CharacterEvent = new CharacterEvent(CharacterEvent.ADD_CHARACTER_FINISH, vo);
			mvcEvent.data = {popUp: this};
			CairngormEventDispatcher.getInstance().dispatchEvent(mvcEvent);
		}

		protected function btnCancel_clickHandler(event:MouseEvent):void
		{
			var mvcEvent:CharacterEvent = new CharacterEvent(CharacterEvent.ADD_CHARACTER_CANCEL);
			mvcEvent.data = {popUp: this, oldIndex: _oldIndex};
			CairngormEventDispatcher.getInstance().dispatchEvent(mvcEvent);
		}
	}
}